package com.shuai.shuaisecondhand_springboot.mapper;

import com.shuai.shuaisecondhand_springboot.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //sql->登录查询
    @Select("SELECT * FROM user WHERE username=#{username} AND password=#{password}")
    User login(@Param("username") String username, @Param("password") String password);

    //sql->注册
    @Insert("INSERT INTO user(username,password) VALUES (#{username},#{password})")
    Boolean registerUser(@Param("username") String username,@Param("password") String password);

    //sql->查询
    @Select("SELECT * FROM user where is_deleted=0")
    List<User> findAll();

    //sql->根据用户名查询id
    @Select("SELECT id FROM user WHERE username=#{username} and is_deleted=0")
    String findId(@Param("username") String username);

    //sql->根据id查询所有数据
    @Select("SELECT * FROM user WHERE id=#{id} and is_deleted=0")
    User findOne(@Param("id") Integer id);

    //mybatis的xml方式用sql->插入
    @Insert("INSERT INTO user(username, sex, phone, address)VALUES (#{username},#{sex},#{phone},#{address})")
    Boolean insertUser(User user);

    @Update("UPDATE user SET username=#{username},sex=#{sex},phone=#{phone},address=#{address} WHERE id=#{id}")
    //mybatis的xml方式用sql->修改
    Boolean updateUser(User user);

    //sql->修改个人密码
    @Update("UPDATE user SET password=#{newpassword} WHERE password=#{password} AND id=#{id}")
    Boolean updatePassword(@Param("id") Integer id,@Param("password") String password,@Param("newpassword") String newpassword);

    //sql->逻辑删除
    @Update("update user SET is_deleted=1 WHERE id=#{id}")
    Integer deleteUser(long id);

    //mybatis的xml方式用sql->批量删除
    Integer deleteUserBatch(List<Integer> ids);

    //sql->分页查询
    @Select("SELECT *  FROM user WHERE (IFNULL(username,'') LIKE concat('%', #{username}, '%')) and is_deleted=0 limit #{pageNum}, #{pageSize}")
    List<User> getUserPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("username") String username);

    //sql->查询所有数据量
    @Select("SELECT count(*) FROM user WHERE (IFNULL(username,'') LIKE concat('%', #{username}, '%'))")
    int getTotal(@Param("username") String username);

    //sql->查询用户总数
    @Select("SELECT COUNT(*) FROM user where is_deleted=0")
    int getAllTotal();
}
